<template>
  <div class="admin-header">
    <div class="header-title">
      教育平台录播课商城
    </div>
    <div class="header-actions">
      <span>欢迎您：{{ username }}</span>
      <!-- 新增的切换暗黑模式按钮 -->
      <el-button type="text" @click="toggleDarkMode">
        {{ isDark ? '切换亮色模式' : '切换暗黑模式' }}
      </el-button>
      <el-button type="text" @click="handlePasswordChange">修改密码</el-button>
      <el-button type="text" @click="handleLogout">退出登录</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
// 导入 useThemeStore
import { useThemeStore } from '@/stores/theme'

const themeStore = useThemeStore()
const isDark = ref(themeStore.darkMode)

// 监听暗黑模式变化
watch(
  () => themeStore.darkMode,
  (newVal) => {
    isDark.value = newVal
  }
)

const username = ref('账号') // 这里可以动态获取用户名
const router = useRouter()

const handlePasswordChange = () => {
  ElMessageBox.alert('请在此处实现修改密码的逻辑', '提示', {
    confirmButtonText: '确定',
  })
}

const handleLogout = () => {
  ElMessageBox.confirm('确认退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    // 退出登录的逻辑，例如清除 token 并跳转到登录页
    localStorage.removeItem('token')
    router.push('/login')
  }).catch(() => {
    // 取消操作
  })
}

// 切换暗黑模式的方法
const toggleDarkMode = () => {
  themeStore.toggleDark()
}
</script>

<style scoped>
.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 0 20px;
  height: 60px;
  background-color: #409EFF;
  color: #fff;
  box-sizing: border-box;
}

.header-title {
  font-size: 20px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  align-items: center;
}

.header-actions span {
  margin-right: 20px;
}

.header-actions .el-button {
  color: #fff;
}
</style>